package com.ssly.media.manage.controller.api;

import com.ssly.media.manage.configuration.MediaProperties;
import com.ssly.media.manage.domain.DownTask;
import com.ssly.media.manage.domain.TargetMedia;
import com.ssly.media.manage.repository.DownTaskRepository;
import com.ssly.media.manage.repository.TargetMediaRepository;
import com.ssly.media.manage.util.JsonResult;
import com.ssly.media.manage.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Optional;


@RestController
@RequestMapping("api/target-media")
public class TargetMediaController {

    @Autowired
    private DownTaskRepository downTaskRepository;
    @Autowired
    private TargetMediaRepository targetMediaRepository;
    @Autowired
    private MediaProperties mediaProperties;

    @GetMapping("/{id}")
    public JsonResult show(@PathVariable Integer id) {
        return R.success();
    }

    @GetMapping("/{id}/download")
    public ResponseEntity<FileSystemResource> download(@PathVariable Integer id) {
        TargetMedia targetMedia = targetMediaRepository.findById(id).get();
        File file = new File(targetMedia.getSavePath());

        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename=" + targetMedia.getName());
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));

    }

    @GetMapping
    public DataTablesOutput<TargetMedia> index(DataTablesInput input) {
        return targetMediaRepository.findAll(input);
    }

    @PutMapping
    public JsonResult update() {
        return R.success();
    }

    @PostMapping
    public JsonResult create(DownTask downTask) {
        return R.success();
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {

        Optional<TargetMedia> optional = targetMediaRepository.findById(id);
        if (optional.isPresent()) {
            TargetMedia targetMedia = optional.get();
            String savePath = targetMedia.getSavePath();
            File file = new File(savePath);
            targetMediaRepository.deleteById(id);
            if (file.delete()) {
                return R.success("删除成功");
            } else {
                return R.success("数据库删除成功,文件删除失败");
            }
        } else {
            return R.error("删除失败");
        }
    }

}
